package indi.lqzhi.basic.ui.basic;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import indi.lqzhi.basic.BasicApp;
import indi.lqzhi.basic.ui.basic.action.ClickAction;
import indi.lqzhi.basic.ui.basic.action.HandlerAction;
import indi.lqzhi.basic.ui.basic.action.LogAction;

public abstract class BaseFragment<Bind extends ViewDataBinding,A extends Activity> extends Fragment implements ClickAction, Handler.Callback, HandlerAction, LogAction {

    protected final String TAG = this.getClass().getSimpleName();

    /**
     * 数据绑定
     */
    protected Bind bindView;

    /**
     * 依附的对象
     */
    protected A activity;

    /**
     * 处理的Handler
     */
    protected Handler handler = new Handler(Looper.myLooper(),this);

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (A) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull  LayoutInflater inflater, @Nullable  ViewGroup container, @Nullable  Bundle savedInstanceState) {
        bindView = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        return bindView.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initFragment();
    }

    @LayoutRes
    public abstract int getLayoutId();

    public abstract void initFragment();

    @Override
    public <T extends View> T findViewById(int id) {
        T t = bindView.getRoot().findViewById(id);
        if (t == null){
            throw new NullPointerException("resource id "+ id +" not find! findViewById error!");
        }
        return t;
    }

    /**
     * 得到附属的Activity
     * @return 附属的Activity
     */
    public A getAttachActivity(){
        return activity;
    }

    public Bind getBindView() {
        return bindView;
    }

    @Override
    public void onDestroyView() {
        removeCallbacks();
        super.onDestroyView();
    }

    /**
     * 销毁当前 Fragment 所在的 Activity
     */
    public void finish() {
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
        }
    }

    @Override
    public Handler getHandler() {
        return handler;
    }

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        //交给子类完成
        return false;
    }

    @Override
    public boolean isDebug() {
        return BasicApp.isDebug();
    }

    @NonNull
    @Override
    public Context getContext() {
        return getAttachActivity();
    }

    public String getTAG() {
        return TAG;
    }
}
